<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\we\model;

use think\Model;

class WeRob extends Model
{

   
    /**
     * 登录/
     */
    public function index($data=''){
    	
       $this->data=$data;
       switch ($data['keyword']) {
          case '@':
            $reply= $this->login();
            break;
          case 'F':
            $reply= $this->info();
            break;
          case 'INFO':
            $reply= $this->info();
            break;
         
          
          default:
            # code...
            break;
        } 



       return $reply;
      
    }


     public function order() {
         $printers=db('printer')->where('aid',$this->data['aid'])->select();
        
         $message='F粉丝(fans)管理工具';
        
         $message.="\n-------------------";
         $message.="\n神奇的大写字母F：\n（F:我是您的粉丝助手，FANS的首字母）";
         $message.="\n/:hug1.查询：\n1F (查询1号粉丝信息)";
         $message.="\n/:hug2.充值：\n1FM1 (给1号粉丝充值1元)";
         $message.="\n/:hug3.积分：\n1FS1 (给1号粉丝集1分)";
        
         return $message;
     }

   
   public function login() {

        $hasUser = db('admin_user')->where(array('mobile' => $this->data['before'],'aid' =>$this->data['aid']))->find();

        if(empty($hasUser)){
            return ['message'=>'管理员不存在'];
        }

        if(md5(md5($this->data['back']) . config('data_auth_key'))!= $hasUser['password']){
           return ['message'=>'密码错误'];
        }

        if(1 != $hasUser['status']){
            return ['message'=>'账号被禁用'];
        }

        $res=db('WeFans')->where('openid',$this->data['openid'])->update(['userid'=>$hasUser['userid']]);
        if ($res)  return ['message'=>$hasUser['name'].'登录成功'];
        return ['message'=>'登录失败'];
    }

     public function info() {

        if ($this->data['userid']==''){
        return ['message'=>'您还没有登录内部员工系统，回复：手机号@密码 登录'];
        }
       
       switch ($this->data['before']) {
         case '':
           return $this->back();
           break;
         case '自己':
           if ($this->data['back']=='') {
             $fans = model('we/WeFans')->infoForFans($this->data['openid']);
             return ['message'=>$fans['message']]; //空尾巴查询
           }
           break;
         case '发送':
          

           break;

       
         
         default:
          return $this->back();
           $fans = model('we/WeFans')->infoForFans($this->data['before']);
           if (!$fans) return ['message'=>'粉丝不存在，请核对粉丝ID'];
           if ($this->data['back']==''){
            return ['message'=>$fans['message']]; //空尾巴查询
           }else{
            
            
           }

           break;
       }

       
    }

    public function back() {
      if ($this->data['back'])  return ['message'=> $this->order()];
      $first=substr($this->data['back'],0,1);


      if (strstr('M',$this->data['back']))

       if (is_numeric($this->data['back'])){
         $res=model('printer/PrinterFans')->pay($fans['fans']['openid'],$this->data['back']);
         return ['message'=>'您为'.$fans['fans']['nickname'].'充值照片'.$this->data['back'].'张'];
       }else{
        $message=['msgtype'=>'text','text'=>['content'=>$this->data['back']]];
        $error= customMessage($fans['fans']['openid'],$message);
        return ['message'=>!$error?'发送成功':$error];
       }



    }

    

    
}